﻿using System;
using System.ServiceModel.DomainServices.Client;

namespace RiaMVVM
{
	/// <summary>
	/// A class for quick RiaQuery creation. Why make a class inheriting from RiaQuery if you don't have to?
	/// </summary>
	/// <typeparam name="TContext"></typeparam>
	public class DelegateRiaQuery<TContext> : RiaQuery<TContext>
		where TContext : DomainContext
	{
		protected Func<TContext, EntityQuery> CreateQueryDelegate;

		public DelegateRiaQuery(TContext context, Func<TContext, EntityQuery> createQueryDelegate)
			: base(context)
		{
			if (createQueryDelegate == null) throw new ArgumentException("createQueryDelegate");
			CreateQueryDelegate = createQueryDelegate;
		}

		protected override EntityQuery CreateQuery()
		{
			return CreateQueryDelegate(Context);
		}
	}

	/// <summary>
	/// A class for quid RiaQuery creation, with strongly typed Entities.
	/// </summary>
	/// <typeparam name="TContext"></typeparam>
	/// <typeparam name="TEntity"></typeparam>
	public class DelegateRiaQuery<TContext, TEntity> : RiaQuery<TContext, TEntity>
		where TContext : DomainContext
		where TEntity : Entity
	{
		protected Func<TContext, EntityQuery<TEntity>> CreateQueryDelegate;

		public DelegateRiaQuery(TContext context, Func<TContext, EntityQuery<TEntity>> createQueryDelegate)
			: base(context)
		{
			if (createQueryDelegate == null) throw new ArgumentException("createQueryDelegate");
			CreateQueryDelegate = createQueryDelegate;
		}

		protected override EntityQuery CreateQuery()
		{
			return CreateQueryDelegate(Context);
		}
	}
}
